/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 *
 * @author Welington
 */
public class FileHandler {
    private static Debug debug = Debug.getInstance();
    private static Class current = FileHandler.class;
    
    public static void save(String fileName, String content, boolean append) {
        try {
            FileWriter fw = new FileWriter(fileName, append);
            fw.write(content);
            fw.close();
        } catch (IOException ex) {
            debug.println(Debug.ERROR, current, ex.getLocalizedMessage());
        }
    }
    
    public static String[] load(String pathName) {
        String lines = "";
        File file = new  File(pathName);
        
        if (!file.exists()) {
            return null;
        }
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            
            try {
                String line;
                
                while ((line = reader.readLine()) != null) {
                    lines += line + "\n";
                }
                
                reader.close();
            } catch (IOException ex) {
                debug.println(Debug.ERROR, current, ex.getLocalizedMessage());
            }
            
        } catch (FileNotFoundException ex) {
            debug.println(Debug.ERROR, current, ex.getLocalizedMessage());
        }
        
        if (lines.trim().equals("")) {
            debug.println(Debug.ERROR, current, "No process description was found.");   
        }
        
        return lines.split("\n");
    }
}
